package cn.jane.rpc.netty.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文件的工具类
 */
public class FileUtil {

    /**
     * 读取 resources 目录下的指定文件，返回文件内容
     *
     * @param resourcePath 指定文件的相对于 resources 目录的路径
     * @return 指定文件的内容
     */
    public static String readResourceToString(String resourcePath) {
        try (InputStream is = FileUtil.class.getClassLoader().getResourceAsStream(resourcePath);
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(Objects.requireNonNull(is), StandardCharsets.UTF_8))) {

            return reader.lines().collect(Collectors.joining(System.lineSeparator()));
        } catch (Exception e) {
            throw new RuntimeException("cannot read resource: " + resourcePath, e);
        }
    }
}
